package com.banzhi.shop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.RequestWithdraw.Tixian_Data;
import com.banzhi.shop.model.CallBack.RequestWinthdrawCallBack;
import com.banzhi.shop.model.IRequestWithdrawModel;
import com.banzhi.shop.model.Impl.RequestWithdrawImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;

import org.json.JSONObject;

/**
 * 申请提现
 */
public class RequestWithdrawActivity extends BaseActivity implements View.OnClickListener, RequestWinthdrawCallBack {
    /**
     * 退出
     */
    private ImageView ivback;
    /**
     * 余额
     */
    private TextView tvyue;
    /**
     * 提现说明
     */
    private TextView tv;
    /**
     * 提现记录
     */
    private TextView tvjilu;
    /**
     * 传过来的余额
     */
    private String yue;
    private Button btn_true;
    /**
     * 提现输入框
     */
    private EditText ettixian;
    private IRequestWithdrawModel model;

    private boolean istixian;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request_withdraw);
        Translucent.setTranslucent(this);
        setinits();
        setListeners();
        Intent intent = getIntent();
        yue = intent.getStringExtra("data");
        if (ASUtils.isCanUse(yue)) {
            tvyue.setText(NumberUtils.DecimalFormat(yue));
        }
        model = new RequestWithdrawImpl();
        model.getShuoming(this);
    }

    private void setinits() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        tvyue = (TextView) findViewById(R.id.tv_yue);
        tv = (TextView) findViewById(R.id.tv_tixian);
        tvjilu = (TextView) findViewById(R.id.tv_jilu);
        btn_true = (Button) findViewById(R.id.btn_true);
        ettixian = (EditText) findViewById(R.id.et_tixian);
        ettixian.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        btn_true.setOnClickListener(this);
        tvjilu.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_true:
                if (!istixian) {
                    istixian = true;
                    String num = ettixian.getText().toString().trim();
                    if (!ASUtils.isCanUse(num)) {
                        ToastUtils.makeToastShort("请输入提现金额");
                    } else if (Double.parseDouble(num) <= 0) {
                        ToastUtils.makeToastShort("提现金额必须大于0元");
                    } else if (Double.parseDouble(num) > Double.parseDouble(yue)) {
                        ToastUtils.makeToastShort("提现金额不得大于余额");
                    } else {
                        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
                        model.getData(num, this);
                    }
                }
                break;
            case R.id.tv_jilu:
                Intent jvintent = new Intent(this, WithdrawDepositListActivity.class);
                startActivity(jvintent);
                break;

        }
    }

    /**
     * @param object 申请提现成功
     */
    @Override
    public void ongetDataSuccess(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        ToastUtils.makeToastShort("提现申请成功");
        istixian=false;
        finish();
    }

    /**
     * @param object 申请提现失败
     */
    @Override
    public void ongetDataError(Object object) {
        istixian=false;
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    /**
     * @param object 获取提现说明成功
     */
    @Override
    public void ongetshuomingSuccess(Object object) {
        Tixian_Data data = (Tixian_Data) object;
        tv.setText(Html.fromHtml(data.getNotice()));
    }

    /**
     * @param object 获取提现说明失败
     */
    @Override
    public void ongetshuomingError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

}
